package Client;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TabbedSubject extends JPanel implements KeyListener {

	private static final long serialVersionUID = 1L;
	String name;
	JPanel midPanel;
	JPanel botPanel;
	JTextArea textArea;
	JTextField textField;
	JScrollPane textAreaScrolled;
	MainWindow mainWindow;
	
	JButton sendButton, quitButton;
	
	public TabbedSubject(MainWindow w, String tabbedName) {
		mainWindow = w;
		name = tabbedName;
		this.setLayout(new BorderLayout());
		midPanel = new JPanel();
		botPanel = new JPanel(new GridLayout());
		
		sendButton = new JButton("Send");
		quitButton = new JButton("Quit");
		textField = new JTextField();
		textField.addKeyListener(this);
		
		textArea = new JTextArea("   Welcome to " + name + "\n\n");
		textArea.setEditable(false);
		textAreaScrolled = new JScrollPane(textArea);
		botPanel.add(textField);
		botPanel.add(sendButton);
		botPanel.add(quitButton);
		
		
		this.add(botPanel, BorderLayout.SOUTH);
		this.add(textAreaScrolled, BorderLayout.CENTER);
		
		buildListeners();

	}
	
	public void buildListeners(){
		/*
		 * Send the message to the server which must diffuse to other client who follow this subject
		 */
		sendButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				sendMessageToServer();
			}
		});
		
		quitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeSubject();
			}
		});
	}
	
	private void closeSubject(){
		mainWindow.closeSubject(name);
	}
	
	/**
	 * Give the name of the client
	 */
	public String getName(){
		return name;
	}
	
	public void appendText(String text){
		String appendText = text;
		int i=1;
		int widhtSize = 120;
		/*
		 * Add line break if the message is too long for the GUI
		 */
		while(appendText.length()> i*widhtSize){
			appendText = appendText.substring(0, widhtSize*i)+"\n"+appendText.substring(widhtSize*i, appendText.length());
			i++;
		}
		this.textArea.append(appendText+'\n');
		textAreaScrolled.getVerticalScrollBar().setValue(textAreaScrolled.getVerticalScrollBar().getMaximum());
	}
	
	private void sendMessageToServer() {
		try {
			// signal au client qu'il faut ecrire du text (getText()) dans le sujet correspondant (name)
			mainWindow.diffuserMessage(name,textField.getText());
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
		textField.setText("");
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyCode()==KeyEvent.VK_ENTER){
			sendMessageToServer();
		}
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}

}
